using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Net;
using System.Globalization;
using DDay.iCal;
using DDay.iCal.Components;
using DDay.iCal.DataTypes;


namespace TdlIcal2Appt
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
    class TdlIcal2Appt
	{

        private static iCalendarCollection _Calendars = new iCalendarCollection();
        private static CalendarCollection CalsCollection = new CalendarCollection();
   
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
           
            LoadCalendars();

            // Get a list of event occurrences from each of our calendars.            
            List<Occurrence> occurrences = _Calendars.GetOccurrences<Event>(DateTime.Now, DateTime.Now.AddDays(10));
            
            // write out to CSV
            StreamWriter sw = new StreamWriter(Properties.TdlIcal2Appt.Default.AppointmentCsvFile.ToString(),false);


            // write out header
            sw.WriteLine("\"Message\",\"Date\",\"Time\",\"Recurrence\",\"Alert\",\"Status\"");


            // Iterate through each occurrence
            foreach (Occurrence o in occurrences)
            {
                // Cast the component to an event
                Event evt = o.Component as Event;
                if (evt != null)
                {
                    // Make sure the event is active (hasn't been cancelled)
                    if (evt.IsActive())
                    {
                        // write out the event

                        // Message - title 
                        sw.Write("\"" + evt.Summary.ToString() + "\", ");

                        // Date          

                        sw.Write("\"" + o.Period.StartTime.Year + "/" + o.Period.StartTime.Month + "/" + o.Period.StartTime.Day + "\", ");

                        // Time
                        if (evt.IsAllDay)
                        {
                            sw.Write("\"07:00:00\", ");
                        }
                        else
                        {
                            sw.Write("\"" + o.Period.StartTime.TimeOfDay + "\", ");
                        }

                        // Recurrance
                        sw.Write("\"0\", ");

                        // Alert - 5 minutes prior
                        sw.Write("\"1\", ");

                        // Armed - true by default
                        sw.WriteLine("Armed");
                    }
                   
                }
            }

            sw.Close();           
		}

        private static void LoadCalendars()
        {
            foreach (string FileToLoad in CalsCollection.CalsOnDisk.Keys)
            {
                if (CalsCollection.CalsOnDisk[FileToLoad].ToString().Length > 0)
                {
                    try
                    {
                        _Calendars.Add(iCalendar.LoadFromFile(CalsCollection.CalsOnDisk[FileToLoad].ToString()));
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("Cannot load: " + FileToLoad);
                    }
                }
            }
        }
	}
}
